I have been trying to figure out why when I run this code nothing happens, there are no errors being caught by VS, and then app opens a console window and then it closes rapidly I do not see anything in the console window, but there are no attachments downloaded and nothing happens.
What I'm attempting to do is use the EWS 2.0 to download the email attachment to the c drive folder temp and I only need the most recent email .
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Exchange.WebServices.Data; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; namespace GetAttachments { class Program { static void Main(string[] args) { ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); service.EnableScpLookup = false; service.Credentials = new WebCredentials("USN", "Password", "domain"); service.TraceEnabled = true; service.TraceFlags = TraceFlags.All; ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack; service.Url = new Uri("https://<IP address of Exchange Server>/ews/exchange.asmx"); } public static void GetAttachmentsFromEmail(ExchangeService service, ItemId itemId) { // Bind to an existing message item and retrieve the attachments collection. // This method results in an GetItem call to EWS. EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(ItemSchema.Attachments)); // Iterate through the attachments collection and load each attachment. foreach (Attachment attachment in message.Attachments) { if (attachment is FileAttachment) { FileAttachment fileAttachment = attachment as FileAttachment; // Load the attachment into a file. // This call results in a GetAttachment call to EWS. fileAttachment.Load("C:\\temp\\" + fileAttachment.Name); Console.WriteLine("File attachment name: " + fileAttachment.Name); } else // Attachment is an item attachment. { ItemAttachment itemAttachment = attachment as ItemAttachment; // Load attachment into memory and write out the subject. // This does not save the file like it does with a file attachment. // This call results in a GetAttachment call to EWS. itemAttachment.Load(); Console.WriteLine("Item attachment name: " + itemAttachment.Name); } } } public static void SaveEmailAttachment(ExchangeService service, ItemId itemId) { // Bind to an existing message item and retrieve the attachments collection. // This method results in an GetItem call to EWS. EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(ItemSchema.Attachments)); foreach (Attachment attachment in message.Attachments) { if (attachment is ItemAttachment) { ItemAttachment itemAttachment = attachment as ItemAttachment; itemAttachment.Load(ItemSchema.MimeContent); string fileName = "C:\\temp\\" + itemAttachment.Item.Subject + ".eml"; // Write the bytes of the attachment into a file. System.IO.File.WriteAllBytes(fileName, itemAttachment.Item.MimeContent.Content); Console.WriteLine("Email attachment name: " + itemAttachment.Item.Subject + ".eml"); } } } private static bool CertificateValidationCallBack( object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { // If the certificate is a valid, signed certificate, return true. if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None) { return true; } // If there are errors in the certificate chain, look at each error to determine the cause. if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0) { if (chain != null && chain.ChainStatus != null) { foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus) { if ((certificate.Subject == certificate.Issuer) && (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot)) { // Self-signed certificates with an untrusted root are valid. continue; } else { if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError) { // If there are any other errors in the certificate chain, the certificate is invalid, // so the method returns false. return false; } } } } // When processing reaches this line, the only errors in the certificate chain are // untrusted root errors for self-signed certificates. These certificates are valid // for default Exchange server installations, so return true. return true; } else { // In all other cases, return false. return true; } } } }